package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.bean.Employee;
import com.itheima.common.PageParam;
import com.itheima.dao.EmployeeDao;
import com.itheima.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;

    /**
     * 后台用户登录
     *
     * @param employee
     * @return
     */
    @Override
    public Employee login(Employee employee) {

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Employee::getUsername, employee.getUsername());

        return employeeDao.selectOne(lqw);

    }

    /**
     * 添加用户
     *
     * @param employee
     * @return
     */
    @Override
    public int add(Employee employee) {
        return employeeDao.insert(employee);
    }

    /**
     * 用户分页查询
     *
     * @param pageParam
     * @return
     */
    @Override
    public IPage findPage(PageParam pageParam) {
        IPage<Employee> employeePage = new Page<>(pageParam.getPage(), pageParam.getPageSize());

        LambdaQueryWrapper<Employee> lqw = new LambdaQueryWrapper<>();

        lqw.like(pageParam.getName()!=null, Employee::getName, pageParam.getName());

        return employeeDao.selectPage(employeePage, lqw);
    }

    /**
     * 更新用户
     *
     * @param employee
     * @return
     */
    @Override
    public int update(Employee employee) {
      return employeeDao.updateById(employee);
    }


}
